package com.example.hp.recyclerviewdata_322jzy.adapter;

import android.content.Context;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.hp.recyclerviewdata_322jzy.R;
import com.example.hp.recyclerviewdata_322jzy.bean.ArticleBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by yubo on 2017/4/13.
 */

public class MyAdapter extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    private List<ArticleBean> list;//保存要显示的数据
    //自定义 构造方法 传进上下文
     public MyAdapter(Context context) {
         this.context=context;
         layoutInflater=LayoutInflater.from(context);
    }
    //自定义：获取数据
    public  void setList(List<ArticleBean> list){

        this.list=list;
    }
     //自定义：ViewHodler子类
    public class ViewHolder extends RecyclerView.ViewHolder{
         private ImageView imageView;
         private TextView tvtitle, tvdescript,tvtime;
         private Button button;
         public ViewHolder(View itemView) {
             super(itemView);
             imageView = (ImageView) itemView.findViewById(R.id.imageView);
             tvtitle = (TextView) itemView.findViewById(R.id.textView);
             tvdescript = (TextView) itemView.findViewById(R.id.textView2);
             tvtime = (TextView) itemView.findViewById(R.id.textView3);
             button = (Button) itemView.findViewById(R.id.button);
         }


     }
    //重写：生成Itemde1View
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v=layoutInflater.inflate(R.layout.item,parent,false);
        return new ViewHolder(v);
    }
    //重写 给ViewHodler中的控件填充值 设置监听
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ArticleBean articleBean = list.get(position);
        if (articleBean == null)
            return;

            ViewHolder viewHolder = (ViewHolder)holder;

            viewHolder.tvtitle.setText(articleBean.getName());
            viewHolder.tvdescript.setText(articleBean.getDescription());
            viewHolder.tvtime.setText(articleBean.getThumb());

        //皮卡搜
       // context已经定义了？？？？ load下载？？？是一个相对地址
        Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"//网络地址
                        +articleBean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);//往哪里放  比如说放一个自己的图片
            viewHolder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.remove(position);
                    notifyDataSetChanged();//更新RecyclerView
                    //在实际案例中 要删除也应该涉及到网络或数据库访问
                    //一般要有删除前的提醒

                }
            });
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //获取当前条目信息   如id
                    int id = list.get(position).getId();
                    Toast.makeText(context, "" + id, Toast.LENGTH_SHORT).show();
                    //实际中 点击后开新窗口做其他条件

                }
            });
        }


    //共由多少条目
    @Override
    public int getItemCount() {
        //改动
        if(list==null) return 0;
        else
        //程序崩溃了 可能 if(list==null) return 0;
       // else没加  控指针错误


        return list.size();
    }
}
